package sea.simulate;

import sea.util.PublicParam;
import sea.util.SimHyAHelper;
import sea.util.WinHelper;

/**
 *
 * This class builds the layout of Trajectory interface of Save
 * 
 * @author  Zhirun Ding
 * 
 * Created on 04/07/2012
 * 
 */

public class SimulateSTrajectoryForm extends javax.swing.JDialog {
    
    /** Creates new form SimuldateSTrasectoryForm */
    public SimulateSTrajectoryForm(java.awt.Frame parent, boolean modal) {
        super(parent, modal);
        initComponents();
    }
    
    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        trasectoryTypeButtonGroup = new javax.swing.ButtonGroup();
        jLabel1 = new javax.swing.JLabel();
        allRadioButton = new javax.swing.JRadioButton();
        singleRadioButton = new javax.swing.JRadioButton();
        fromRadioButton = new javax.swing.JRadioButton();
        listRadioButton = new javax.swing.JRadioButton();
        backButton = new javax.swing.JButton();
        nextButton = new javax.swing.JButton();
        singleText = new javax.swing.JTextField();
        fromText = new javax.swing.JTextField();
        jLabel2 = new javax.swing.JLabel();
        toText = new javax.swing.JTextField();
        listText = new javax.swing.JTextField();
        helpButton = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        org.jdesktop.application.ResourceMap resourceMap = org.jdesktop.application.Application.getInstance(sea.SimHyAGui.class).getContext().getResourceMap(SimulateSTrajectoryForm.class);
        setTitle(resourceMap.getString("SimulateSTrasectoryForm.title")); // NOI18N
        setName("SimulateSTrasectoryForm"); // NOI18N
        setResizable(false);
        addComponentListener(new java.awt.event.ComponentAdapter() {
            public void componentShown(java.awt.event.ComponentEvent evt) {
                formComponentShown(evt);
            }
        });

        jLabel1.setText(resourceMap.getString("jLabel1.text")); // NOI18N
        jLabel1.setName("jLabel1"); // NOI18N

        trasectoryTypeButtonGroup.add(allRadioButton);
        allRadioButton.setSelected(true);
        allRadioButton.setText(resourceMap.getString("allRadioButton.text")); // NOI18N
        allRadioButton.setName("allRadioButton"); // NOI18N
        allRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                allRadioButtonItemStateChanged(evt);
            }
        });

        trasectoryTypeButtonGroup.add(singleRadioButton);
        singleRadioButton.setText(resourceMap.getString("singleRadioButton.text")); // NOI18N
        singleRadioButton.setName("singleRadioButton"); // NOI18N
        singleRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                singleRadioButtonItemStateChanged(evt);
            }
        });

        trasectoryTypeButtonGroup.add(fromRadioButton);
        fromRadioButton.setText(resourceMap.getString("fromRadioButton.text")); // NOI18N
        fromRadioButton.setName("fromRadioButton"); // NOI18N
        fromRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                fromRadioButtonItemStateChanged(evt);
            }
        });
        fromRadioButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                fromRadioButtonActionPerformed(evt);
            }
        });

        trasectoryTypeButtonGroup.add(listRadioButton);
        listRadioButton.setText(resourceMap.getString("listRadioButton.text")); // NOI18N
        listRadioButton.setName("listRadioButton"); // NOI18N
        listRadioButton.addItemListener(new java.awt.event.ItemListener() {
            public void itemStateChanged(java.awt.event.ItemEvent evt) {
                listRadioButtonItemStateChanged(evt);
            }
        });

        backButton.setText(resourceMap.getString("backButton.text")); // NOI18N
        backButton.setName("backButton"); // NOI18N
        backButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                backButtonActionPerformed(evt);
            }
        });

        nextButton.setText(resourceMap.getString("nextButton.text")); // NOI18N
        nextButton.setName("nextButton"); // NOI18N
        nextButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                nextButtonActionPerformed(evt);
            }
        });

        singleText.setText(resourceMap.getString("singleText.text")); // NOI18N
        singleText.setName("singleText"); // NOI18N

        fromText.setText(resourceMap.getString("fromText.text")); // NOI18N
        fromText.setName("fromText"); // NOI18N

        jLabel2.setText(resourceMap.getString("jLabel2.text")); // NOI18N
        jLabel2.setName("jLabel2"); // NOI18N

        toText.setText(resourceMap.getString("toText.text")); // NOI18N
        toText.setName("toText"); // NOI18N

        listText.setText(resourceMap.getString("listText.text")); // NOI18N
        listText.setName("listText"); // NOI18N
        listText.setPreferredSize(new java.awt.Dimension(72, 21));

        helpButton.setText(resourceMap.getString("helpButton.text")); // NOI18N
        helpButton.setName("helpButton"); // NOI18N
        helpButton.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                helpButtonActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(layout.createSequentialGroup()
                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(layout.createSequentialGroup()
                                .addComponent(singleRadioButton)
                                .addGap(6, 6, 6)
                                .addComponent(singleText, javax.swing.GroupLayout.PREFERRED_SIZE, 97, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jLabel1)
                            .addGroup(layout.createSequentialGroup()
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(allRadioButton)
                                        .addGap(24, 24, 24))
                                    .addGroup(layout.createSequentialGroup()
                                        .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                                            .addComponent(listRadioButton)
                                            .addComponent(fromRadioButton))
                                        .addGap(18, 18, 18)))
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                    .addGroup(layout.createSequentialGroup()
                                        .addComponent(fromText, javax.swing.GroupLayout.DEFAULT_SIZE, 42, Short.MAX_VALUE)
                                        .addGap(18, 18, 18)
                                        .addComponent(jLabel2)
                                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                                        .addComponent(toText, javax.swing.GroupLayout.PREFERRED_SIZE, 88, javax.swing.GroupLayout.PREFERRED_SIZE)
                                        .addGap(97, 97, 97))
                                    .addComponent(listText, javax.swing.GroupLayout.PREFERRED_SIZE, 206, javax.swing.GroupLayout.PREFERRED_SIZE))))
                        .addContainerGap())
                    .addGroup(javax.swing.GroupLayout.Alignment.TRAILING, layout.createSequentialGroup()
                        .addComponent(backButton, javax.swing.GroupLayout.PREFERRED_SIZE, 70, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(22, 22, 22)
                        .addComponent(nextButton)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addComponent(helpButton)
                        .addGap(30, 30, 30))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(layout.createSequentialGroup()
                .addContainerGap()
                .addComponent(jLabel1)
                .addGap(18, 18, 18)
                .addComponent(allRadioButton)
                .addGap(10, 10, 10)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(singleRadioButton)
                    .addComponent(singleText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(fromRadioButton)
                    .addComponent(fromText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel2)
                    .addComponent(toText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(listRadioButton)
                    .addComponent(listText, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(layout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(helpButton)
                    .addComponent(backButton)
                    .addComponent(nextButton))
                .addGap(20, 20, 20))
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void backButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_backButtonActionPerformed
        this.setVisible(false);
        
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(true);
        }   
}//GEN-LAST:event_backButtonActionPerformed

    private void nextButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_nextButtonActionPerformed
        //run command of save
        //generate plot command expression
        toSaveCommand();       
        //execute command
        this.setVisible(false);
        SimHyAHelper.executeCommand();       
        //close this form
        this.dispose();        
        //clear all form of plot
        WinHelper.closeFormAfterCommand();
    }//GEN-LAST:event_nextButtonActionPerformed

    private void formComponentShown(java.awt.event.ComponentEvent evt) {//GEN-FIRST:event_formComponentShown
        if (PublicParam.PreviousForm != null) {
            PublicParam.PreviousForm.setVisible(false);
        }
    }//GEN-LAST:event_formComponentShown

    private void helpButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_helpButtonActionPerformed
        WinHelper.showHelp("save/trajectory");
    }//GEN-LAST:event_helpButtonActionPerformed

    private void allRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_allRadioButtonItemStateChanged
        singleText.setEnabled(!allRadioButton.isSelected());
        fromText.setEnabled(!allRadioButton.isSelected());
        toText.setEnabled(!allRadioButton.isSelected());
        listText.setEnabled(!allRadioButton.isSelected());        
    }//GEN-LAST:event_allRadioButtonItemStateChanged

    private void singleRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_singleRadioButtonItemStateChanged
        singleText.setEnabled(singleRadioButton.isSelected());
        fromText.setEnabled(!singleRadioButton.isSelected());
        toText.setEnabled(!singleRadioButton.isSelected());
        listText.setEnabled(!singleRadioButton.isSelected());
    }//GEN-LAST:event_singleRadioButtonItemStateChanged

    private void fromRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_fromRadioButtonItemStateChanged
        singleText.setEnabled(!fromRadioButton.isSelected());
        fromText.setEnabled(fromRadioButton.isSelected());
        toText.setEnabled(fromRadioButton.isSelected());
        listText.setEnabled(!fromRadioButton.isSelected());
    }//GEN-LAST:event_fromRadioButtonItemStateChanged

    private void listRadioButtonItemStateChanged(java.awt.event.ItemEvent evt) {//GEN-FIRST:event_listRadioButtonItemStateChanged
        // TODO add your handling code here:
    }//GEN-LAST:event_listRadioButtonItemStateChanged

    private void fromRadioButtonActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_fromRadioButtonActionPerformed
        // TODO add your handling code here:
    }//GEN-LAST:event_fromRadioButtonActionPerformed
    
    void toSaveCommand() {
        String s = "";
        
        //trajectoryRadioButton
        if (singleRadioButton.isSelected() && singleText.getText().length() > 0) {
            s += ",trajectory=" + singleText.getText();
        } else if (allRadioButton.isSelected()) {                
            s += ",trajectory=all";
        } else if (fromRadioButton.isSelected()  && fromText.getText().length() > 0  && toText.getText().length() > 0) {                
            s += ",trajectory=" + fromText.getText() + ":" + toText.getText();
        } else if (listRadioButton.isSelected() && listText.getText().length() > 0) {
            s += ",trajectory=[" + listText.getText() + "]";
        }
        
        PublicParam.SimHyACommandExpression += s + ")";
        PublicParam.CommandTypeName = "Save"; 
    }
    
    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
                SimulateSTrajectoryForm dialog = new SimulateSTrajectoryForm(new javax.swing.JFrame(), true);
                dialog.addWindowListener(new java.awt.event.WindowAdapter() {
                    public void windowClosing(java.awt.event.WindowEvent e) {
                        System.exit(0);
                    }
                });
                dialog.setVisible(true);
            }
        });
    }
    
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JRadioButton allRadioButton;
    private javax.swing.JButton backButton;
    private javax.swing.JRadioButton fromRadioButton;
    private javax.swing.JTextField fromText;
    private javax.swing.JButton helpButton;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JRadioButton listRadioButton;
    private javax.swing.JTextField listText;
    private javax.swing.JButton nextButton;
    private javax.swing.JRadioButton singleRadioButton;
    private javax.swing.JTextField singleText;
    private javax.swing.JTextField toText;
    private javax.swing.ButtonGroup trasectoryTypeButtonGroup;
    // End of variables declaration//GEN-END:variables
    
}
